package com.erp.Struts.Action;

import java.util.Date;

import com.erp.Struts.common.Constants;
import com.erp.hibernate.beans.Employee;
import com.erp.hibernate.beans.Outbill;
import com.erp.hibernate.dao.OutbillDAO;

public class OutBillAction extends PageAction {

	private static final long serialVersionUID = -2127762902613648206L;
	private OutbillDAO outbillDAO;
	private Outbill outbill;
	

	public String init() throws Exception{
		super.pageSize = Constants.pageSize;
		super.pageNo = Constants.pageNo;
		super.pager = this.getOutbillDAO().findPager(super.pageSize, super.pageNo);
		setSession(Constants.PAGER_INBILL, super.pager);
		return Constants.LIST_KEY;
	}
	
	public String list() throws Exception{
		super.pager = this.getOutbillDAO().findPager(super.pageSize, super.pageNo);
		setSession(Constants.PAGER_INBILL, super.pager);
		return Constants.LIST_KEY;
	}
	
	public String add() throws Exception{
		reset();
		return Constants.ADD_KEY;
	}
	
	public String insert() throws Exception{
		outbill.setDate(new Date());
		outbill.setEmployee((Employee)getSession(Constants.EMPLOYEE_KEY));
		this.getOutbillDAO().save(outbill);

		super.pageSize = Constants.pageSize;
		super.pageNo = Constants.pageNo;
		super.pager = this.getOutbillDAO().findPager(super.pageSize, super.pageNo);
		setSession(Constants.PAGER_INBILL, super.pager);
		return Constants.LIST_KEY;
	}
	
	public String edit() throws Exception{
		if (this.getOutbill().getId() == null) {
			return Constants.LIST_KEY;
		} else {
			Outbill bill2 = this.getOutbillDAO().findById(this.getOutbill().getId());

			if (bill2 == null) {
				return Constants.LIST_KEY;
			} else {
				outbill = bill2;
				return Constants.EDIT_KEY;
			}
		}
	}
	
	public String saveedit() throws Exception{
		this.getOutbillDAO().update(outbill);

		super.pageSize = Constants.pageSize;
		super.pageNo = Constants.pageNo;
		super.pager = this.getOutbillDAO().findPager(super.pageSize, super.pageNo);
		setSession(Constants.PAGER_INBILL, super.pager);
		return Constants.LIST_KEY;
	}
	
	private void reset() {
		outbill = null;
	}

	public OutbillDAO getOutbillDAO() {
		return outbillDAO;
	}

	public void setOutbillDAO(OutbillDAO outbillDAO) {
		this.outbillDAO = outbillDAO;
	}

	public Outbill getOutbill() {
		return outbill;
	}

	public void setOutbill(Outbill outbill) {
		this.outbill = outbill;
	}

}
